package com.sacc.controller;


import com.google.gson.Gson;
import com.power.common.model.CommonResult;
import com.sacc.clients.BoxClient;
import com.sacc.pojo.BoxAll;
import com.sacc.pojo.BoxCheck;
import com.sacc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * @author 滚~韬
 * @date 2022/4/12 13:26
 */
@RestController
@RequestMapping("/box/box")
public class BoxAllController {
    @Autowired
    private BoxAllService boxAllService;
    @Autowired
    private BoxClient boxClient;
    @Autowired
    private BoxProductService boxProductService;
    @Autowired
    private BoxCollectedService boxCollectedService;
    @Autowired
    private BoxAdoptedService boxAdoptedService;
    @Autowired
    private BoxInfoService boxInfoService;
    /**
     * 根据box_id获取boxcheck
     * @param boxId
     * @return
     * @throws IOException
     */
    @GetMapping("/get")
    BoxAll getBox(@RequestParam Integer boxId) throws IOException {
        System.out.println("调用");
        return boxAllService.getBox(boxId);
    }

    /**
     * 根据box_id获取部分boxcheck
     * @param boxId
     * @return
     * @throws IOException
     */
    @GetMapping("/getpart")
    BoxCheck getPart(@RequestParam Integer boxId) throws IOException {
        return boxAllService.getPartofBox(boxId);
    }

    /**
     * 获取用户已经认养蜂箱详情
     * @param openId
     * @return
     */
    @GetMapping("/getadopted")
    List<BoxCheck> getadoptedBox(@RequestParam String openId){
        List<Integer> integers = boxClient.boxIdsadopted(openId);
        if(integers.isEmpty()){
            return Collections.EMPTY_LIST;
        }
        List<BoxCheck> boxAllsById = boxAllService.getBoxAllsById(integers);
        boxAllsById.forEach(b->{
            b.setState(1);
        });
        return boxAllsById;
    }

    /**
     * 获取用户待认养蜂箱详情
     * @param openId
     * @return
     */
    @GetMapping("/getunadopted")
    List<BoxCheck> getunadoptedBox(@RequestParam String openId){
        List<Integer> integersUn = new ArrayList<>(boxClient.boxIdsUnadopted(openId));//收藏的boxid
        //如果没有收藏蜂箱,那么就返回null
        if(integersUn.isEmpty()){
            return Collections.EMPTY_LIST;
        }
        List<Integer> integers = boxClient.boxIdsadopted(openId);//领养的boxid
        //如果有收藏蜂箱,没有领养蜂箱,直接返回
        if(integers.isEmpty()){
            List<BoxCheck> boxAllsById = boxAllService.getBoxAllsById(integersUn);
            boxAllsById.forEach(b->{
                b.setState(0);
            });
            return boxAllsById;
        }
        //收藏过滤已经领养的
        integers.forEach(i->{
            if (integersUn.contains(i)) {
                integersUn.remove(Integer.valueOf(i));
            }
        });
        List<BoxCheck> boxAllsById = boxAllService.getBoxAllsById(integersUn);
        boxAllsById.forEach(b->{
            b.setState(0);
        });
        return boxAllsById;
    }

    /**
     * 获取用户所有蜂箱详情(包括待认养和已经认养)
     * @param openId
     * @return
     */
    @GetMapping("/getallcheck")
    List<BoxCheck> getAllCheck(@RequestParam String openId){
        List<BoxCheck> boxCheckList = new ArrayList<>();
        List<BoxCheck> boxChecks = getadoptedBox(openId);
        List<BoxCheck> boxChecksUn = getunadoptedBox(openId);
        boxCheckList.addAll(boxChecks);
        boxCheckList.addAll(boxChecksUn);
        return boxCheckList;
    }

    /**
     * 获取已经收藏和已经认养的蜂箱数量
     */
    @GetMapping("/getallcheckcount")
    HashMap<String,Integer> getAllCheckCount(@RequestParam String openId){
        HashMap<String,Integer> map = new HashMap<>();
        List<Integer> integers = boxAdoptedService.getBoxIds(openId);
        List<Integer> integersUn = new ArrayList<>(boxCollectedService.getBoxIdsCollected(openId));
        //收藏过滤已经领养的
        integers.forEach(i->{
            if (integersUn.contains(i)) {
                integersUn.remove(Integer.valueOf(i));
            }
        });
        map.put("waitadopted",integersUn.size());
        map.put("adopted",integers.size());
        return map;
    }

    /**
     * 添加一个蜂箱boxall
     * @param boxAll
     * @param files
     * @return
     */
    @PostMapping("/add")
    public CommonResult addOneList(String boxAll, MultipartFile[] files){
        Gson gson = new Gson();
        return boxAllService.addOne(gson.fromJson(boxAll, BoxAll.class),files);
    }

    /**
     * 根据用户id和boxid获取一个蜂箱的具体信息
     * @return
     */
    @GetMapping("/getdetail")
    public String getDetailByBoxId(@RequestParam String openId, @RequestParam Integer boxId) throws IOException {
        Gson gson = new Gson();
        Map<String,Object> tts=new HashMap<String,Object>();
        List<Integer> boxIdsAdopted = boxAdoptedService.getBoxIds(openId);//领养的id
        List<Integer> boxIdsCollected = boxCollectedService.getBoxIdsCollected(openId);//收藏的id
        tts.put("isAdopted",false);
        tts.put("isCollected",false);
        //如果用户领养id里面有boxid,就设置已经领养
        if(boxIdsAdopted.contains(boxId)){
            tts.put("boxinfo",boxInfoService.Get(boxId));
            tts.replace("isAdopted",true);
        }
        //如果用户收藏id里面有boxid,就设置已经收藏
        if(boxIdsCollected.contains(boxId)){
            tts.replace("isCollected",true);
        }
        tts.put("boxcontent",boxInfoService.Get(boxId).getBoxContent());
        tts.putIfAbsent("boxall",boxAllService.getBox(boxId));
        //添加蜂箱产品列表
        tts.put("productlist",boxProductService.getByBoxId(boxId));
        String s = gson.toJson(tts);
        return s;
    }

    /**
     * 添加蜂箱的产品id
     * @param productId
     * @param boxId
     * @return
     */
    @PostMapping("/addid")
    public CommonResult AddId(@RequestParam Integer productId,@RequestParam Integer boxId){
        return boxProductService.AddId(productId, boxId);
    }
}
